//
//  PublicTableView.m
//  Mi_Cet4Listening
//
//  Created by lxl on 2018/8/23.
//  Copyright © 2018年 lxl. All rights reserved.
//

#import "PublicTableView.h"
#import "MJRefresh.h"
#import "maosheng-Swift.h"
@implementation PublicTableView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
- (NSMutableArray *)dataSource{
    if (!_dataSource) {
        _dataSource = [NSMutableArray new];
    }
    return _dataSource;
}
#pragma mark-添加上拉加载，下拉刷新
- (void)addHeaderRefreshFinish:(void(^)(void))_finish{
    //    __unsafe_unretained typeof(self) weakSelf = self;
    _tableView.mj_header= [MJRefreshNormalHeader headerWithRefreshingBlock:^{
    _finish();
    }];
    _tableView.mj_header.automaticallyChangeAlpha = YES;
}
- (void)addFooterRefreshFinish:(void(^)(void))_finish{
    _tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        _finish();
    }];
}
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self addSubview:self.tableView];
        self.tableView.frame = CGRectMake(0, 0, 375, 667);
        _courseWeb = [[CourseWebView alloc] initWithFrame:CGRectMake(0, 0,SCREEN_WIDTH , 100)];

//        [self.tableView ]
//        [self.tableView makeConstraints:^(MASConstraintMaker *make) {
//            make.top.left.right.bottom.equalTo(@0).priority(200);
//        }];
    }
    return self;
}
- (UITableView *)tableView {
    if(!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tableView.backgroundColor = [UIColor whiteColor];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.showsVerticalScrollIndicator = NO;
//        _tableView
        _tableView.estimatedRowHeight = 235;
        _tableView.rowHeight = UITableViewAutomaticDimension;
        _tableView.estimatedSectionHeaderHeight = 0;
        _tableView.estimatedSectionFooterHeight = 0;
        _tableView.tableFooterView = [UIView new];
    }
    return _tableView;
}
- (void)reloadTableView:(CGFloat)height{
    if(webViewHeight != height){
        webViewHeight = height;
        [self.tableView reloadData];
        NSLog(@"web高度%f",height);
    }
    
}
#pragma mark-UITableviewDelegate && UITableviewDataSource
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [UIView new];
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (self.str_desc) {
        static NSString *HeaderIdentifier = @"textView";
        UITableViewHeaderFooterView * view = [tableView dequeueReusableHeaderFooterViewWithIdentifier:HeaderIdentifier];
        if (view == nil){
            view = [[UITableViewHeaderFooterView alloc] initWithReuseIdentifier:HeaderIdentifier];
            __weak typeof(self) wkSelf = self;
            _courseWeb.changeHeaderViewHeight = ^(CGFloat height) {
                [wkSelf reloadTableView:height];
            };
            [_courseWeb addTheCourseWebView:self.str_desc];
            [view addSubview:_courseWeb];
        }
        return view;
    }
    return [UIView new];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (self.str_desc ) {
        CGFloat startHeight = webViewHeight > 0 ? webViewHeight : 100;
        return startHeight ;
    }
    return 0.1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return  self.dataSource.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *CellIdentifier = @"PublicTableView";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    id value = [self.dataSource objectAtIndex:indexPath.row];
    if ([value isKindOfClass:[NSString class]]) {
        cell.textLabel.text = value;
    }
//        id dict = [self.courseMarr objectAtIndex:indexPath.row];
//        [cell setMicroClassInfo:dict With:nil];

    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.didClickRow) {
//        if (self.sourceVCs) {
            self.didClickRow(indexPath.row, [self.dataSource objectAtIndex:indexPath.row]);
//        }
    }
}


//计算文字size
+(CGFloat) boundingRectWithSize:(NSString*) txt Font:(UIFont*) font Size:(CGSize) size{
    
    CGSize _size;
    
    NSDictionary *attribute = @{NSFontAttributeName:font};
    
    NSStringDrawingOptions options = NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading;
    
    _size = [txt boundingRectWithSize:size options: options attributes:attribute context:nil].size;
    _size.width += 1;
    _size.height += 1;
    return _size.height;
    
}
- (void)addTheHeaderView{
    if (!self.str_Title) {
        return;
    }
    CGFloat titleWidth = SCREEN_WIDTH - 30;
    CGFloat titleHeight = [PublicTableView boundingRectWithSize:self.str_Title Font:[UIFont fontWithName:@"PingFangSC-Medium" size:17] Size:CGSizeMake(titleWidth, 1000)];
    
    
    
    _headView = [UIView new];
    _headView.backgroundColor = [UIColor whiteColor];
    self.headerLab = [UILabel new];
    self.headerLab.numberOfLines = 0;
    self.headerLab.text = self.str_Title;
    self.headerLab.font = [UIFont fontWithName:@"PingFangSC-Medium" size:17];
    self.headerLab.textColor = [UIColor colorWithRed:51/255.0 green:51/255.0 blue:51/255.0 alpha:1];
    [self.headView addSubview:self.headerLab];
    self.headerLab.frame = CGRectMake(15, 23, titleWidth, titleHeight);
    
    
    CGFloat imgWidth = 37;
    _avaterImg = [UIImageView new];
    _avaterImg.image = [UIImage imageNamed:@"login_head_moren"];
    _avaterImg.layer.cornerRadius = imgWidth/2;
    _avaterImg.layer.masksToBounds = YES;
    _avaterImg.frame = CGRectMake(15, CGRectGetMaxY( self.headerLab.frame) + 22, imgWidth, imgWidth);
    [self.headView addSubview:_avaterImg];
    
    
    
    
    //    [self.descLab setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical];
    self.nameLab = [UILabel new];
    self.nameLab.text = self.str_userName;
    self.nameLab.font = [UIFont fontWithName:@"PingFangSC-Regular" size:15];
    self.nameLab.textColor = [UIColor colorWithRed:51/255.0 green:51/255.0 blue:51/255.0 alpha:1];
    [self.headView addSubview:self.nameLab];
    self.nameLab.frame = CGRectMake(CGRectGetMaxX(_avaterImg.frame) + 7, CGRectGetMinY(_avaterImg.frame), SCREEN_WIDTH - CGRectGetMaxX(_avaterImg.frame) + 7 - 10, 15);
    
    
    CGFloat detailWidth  = SCREEN_WIDTH - CGRectGetMaxX(_avaterImg.frame) + 7 - 10;
    CGFloat detailHeight = [PublicTableView boundingRectWithSize:self.str_headline Font:[UIFont fontWithName:@"PingFangSC-Medium" size:12] Size:CGSizeMake(detailWidth , 1000)];
    
    self.descLab = [UILabel new];
    self.descLab.numberOfLines = 0;
    self.descLab.text = self.str_headline;
    
    self.descLab.font = [UIFont fontWithName:@"PingFangSC-Regular" size:12];
    self.descLab.textColor = [UIColor colorWithRed:153/255.0 green:153/255.0 blue:153/255.0 alpha:1];
    [self.headView addSubview:self.descLab];
    self.descLab.frame = CGRectMake(CGRectGetMinX(self.nameLab.frame), CGRectGetMaxY(_nameLab.frame) + 9,detailWidth, detailHeight);
    
    
    
    CGFloat headerHeight = CGRectGetMaxY(self.descLab.frame) + 20;
    
    
    
    
//    [self.webview addSubview:_headView];
    _headView.frame = CGRectMake(0, 0, SCREEN_WIDTH, headerHeight);
    self.tableView.tableHeaderView = _headView;
    
//    self.webview.scrollView.contentInset= UIEdgeInsetsMake(headerHeight,0,0,0);
    
}
@end
